#define _CRT_SECURE_NO_WARNINGS 1
#include"Stack.h"
void StackInit(Stack* pst)
{
	pst->a = (Datatype*)malloc(sizeof(Datatype)* 4);
	pst->top = -1;
	pst->capacity = 4;
}
void StackDestory(Stack* pst)
{
	assert(pst);
	free(pst->a);
	pst->a = NULL;
	pst->top = -1;
	pst->capacity = 0;

}
void StackPush(Stack* pst, Datatype e)
{
	assert(pst);
	if (pst->top+1 == pst->capacity)
	{
		Datatype* tmp = (Datatype*)realloc(pst->a, sizeof(Datatype)*pst->capacity * 2);
		if (tmp == NULL)
		{
			exit(-1);
		}
		pst->a = tmp;
		pst->capacity *= 2;
	}
	pst->top++;
	pst->a[pst->top] = e;
}
void StackPop(Stack* pst)
{
	assert(pst);
	assert(!StackEmpty(pst));

	pst->top--;

}
Datatype StackTop(Stack* pst)
{
	assert(pst);
	assert(!StackEmpty(pst));
	return pst->a[pst->top];
}
bool StackEmpty(Stack* pst)
{
	if (pst->top == -1)
	{
		return true;
	}
	else
	{
		return false;
	}
}
int Stacksize(Stack* pst)
{
	return pst->top + 1;
}
bool isValid(char * s)
{
	Stack st1;
	StackInit(&st1);
	while (*s)
	{
		if (*s == '{' || *s == '[' || *s == '(')
		{
			StackPush(&st1, *s);
			s++;
		}
		else
		{
			if (StackEmpty(&st1))
			{
				return false;
			}
			char tmp = StackTop(&st1);
			

			if (*s == '}'&&tmp == '{' ||
				*s == ']'&&tmp == '[' ||
				*s == ')'&&tmp == '(')
			{
				s++;
				StackPop(&st1);
			}
			else
			{
				return false;
			}
		}
	}
	if (StackEmpty(&st1))
	{
		return true;
	}
	else
	{
		return false;
	}
	

	StackDestory(&st1);
}
